#!/bin/sh

. /etc/rc.freenas

cifs_file="/tmp/.cifs_DC"
service=/usr/sbin/service
python=/usr/local/bin/python
notifier=/usr/local/bin/midclt

dcctl_cmd()
{
	local args="$*"

	if [ -n "${args}" ]
	then
		logger -t DomainController "${args}"
		${args}
		return $?
	fi

	return 0
}

cifs_enabled()
{
	srv_enabled cifs && return 0
	return 1
}

cifs_start()
{
	dcctl_cmd ${python} ${notifier} call notifier.start cifs
	return $?
}

cifs_stop()
{
	dcctl_cmd ${python} ${notifier} call notifier.stop cifs
	return $?
}

cifs_restart()
{
	dcctl_cmd ${python} ${notifier} call notifier.stop cifs
	dcctl_cmd ${python} ${notifier} call notifier.start cifs
	return $?
}

get_bindip()
{
	${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "\
	SELECT
		cifs_srv_bindip
	FROM
		services_cifs
	ORDER BY
		-id
	LIMIT
		1
	" | sed 's|,| |g'
}

get_domain()
{
	${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "\
	SELECT
		dc_domain
	FROM
		services_domaincontroller
	ORDER BY
		-id
	LIMIT
		1
	"
}

get_cifs_homedir()
{
	${FREENAS_SQLITE_CMD} ${FREENAS_CONFIG} "\
	SELECT
		cifs_path
	FROM
		sharing_cifs_share
	WHERE
		cifs_home = 1
	ORDER BY
		-sharing_cifs_share.id
	LIMIT
		1
	"
}

setup_homedirs()
{
	local cifs_home="$(get_cifs_homedir)"

	if [ -n "${cifs_home}" ]
	then
		local domain="$(get_domain)"
		local homedir="/var/home/${domain}"

		ln -sfh "${cifs_home}" "/var/home"
		mkdir -p "${homedir}"
	fi
}

dcctl_start()
{
	local cifs_started=0	
	local dc_started=0

	if cifs_enabled
	then
		cifs_started=1
		cifs_stop
	else
		srv_set cifs 1
	fi

	if domaincontroller_enabled
	then
		dc_started=1
	else		
		domaincontroller_set 1
	fi

	echo ${cifs_started} > "${cifs_file}"

	dcctl_cmd ${service} ix-kerberos quietstart
	dcctl_cmd ${service} ix-resolv quietstart
	dcctl_cmd ${service} ix-nsswitch quietstart
	dcctl_cmd ${service} ix-pam quietstart

	setup_homedirs
	cifs_start

	return 0
}

dcctl_stop()
{
	local cifs_started=1
	local prev_cifs_started=0
	if [ -s "${cifs_file}" ]
	then
		prev_cifs_started="$(cat "${cifs_file}")"
	fi

	if ! domaincontroller_enabled
	then
		domaincontroller_set 1
	fi

	if cifs_enabled
	then
		cifs_started=1
		cifs_stop
	fi

	dcctl_cmd ${service} ix-kerberos quietstop
	dcctl_cmd ${service} ix-nsswitch quietstop
	dcctl_cmd ${service} ix-pam quietstop
	dcctl_cmd "${service} ix-cache quietstop &"

	if [ "${prev_cifs_started}" = "0" -a "${cifs_started}" = "0" ]
	then
		dcctl_cmd ${service} samba_server forcestop
		srv_set cifs 0
		domaincontroller_set 0
		dcctl_cmd ${service} ix-pre-samba start

	elif [ "${prev_cifs_started}" = "0" -a "${cifs_started}" = "1" ]
	then
		dcctl_cmd ${service} samba_server forcestop
		srv_set cifs 0
		domaincontroller_set 0
		dcctl_cmd ${service} ix-pre-samba start

	elif [ "${prev_cifs_started}" = "1" -a "${cifs_started}" = "0" ]
	then
		dcctl_cmd ${service} samba_server forcestop
		domaincontroller_set 0
		srv_set cifs 1
		cifs_start

	elif [ "${prev_cifs_started}" = "1" -a "${cifs_started}" = "1" ]		
	then
		dcctl_cmd ${service} samba_server forcestop
		domaincontroller_set 0
		srv_set cifs 1
		cifs_start
	fi

	domaincontroller_set 0
	dcctl_cmd ${service} ix-resolv quietstop

	return 0
}

dcctl_status()
{
	local pidfile="/var/run/samba4/samba.pid"
	if [ -s "${pidfile}" ]
	then
		pgrep -qF "${pidfile}"
		return $?
	fi

	return 1
}

name="dcctl"
start_cmd='dcctl_start'
status_cmd='dcctl_status'
stop_cmd='dcctl_stop'
extra_commands='status'

load_rc_config $name
run_rc_command "$1"
